package com.atguigu.gmall.list.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.service.SearchService;
import com.atguigu.gmall.model.list.Goods;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * @author mgx
 * @version 1.0
 * @description: TODO
 * @date 2022/11/12 22:49
 */
@RestController
@RequestMapping("/api/list")
public class ListApiController {

    // 引入es操作模板对象
    @Autowired
    private ElasticsearchRestTemplate restTemplate;

    @Autowired
    private SearchService searchService;


    /**
     * 创建索引库建立mapping结构
     * /api/list/createIndex
     * @return
     */
    @GetMapping("/createIndex")
    public Result createIndex() {
        // 创建索引
        restTemplate.createIndex(Goods.class);
        // 建立mapping结构
        restTemplate.putMapping(Goods.class);
        return Result.ok();
    }


    /**
     * 商品上架
     * /api/list/inner/upperGoods/{skuId}
     * @param skuId
     * @return
     */
    @GetMapping("/inner/upperGoods/{skuId}")
    public Result upperGoods(@PathVariable Long skuId) {
        searchService.upperGoods(skuId);
        return Result.ok();
    }


    /**
     * 商品下架
     * /api/list/inner/lowerGoods/{skuId}
     * @param skuId
     * @return
     */
    @GetMapping("/inner/lowerGoods/{skuId}")
    public Result lowerGoods(@PathVariable Long skuId) {
        searchService.lowerGoods(skuId);
        return Result.ok();
    }


    /**
     * 更新商品的热度排名
     * /api/list/inner/incrHotScore/{skuId}
     * @param skuId
     * @return
     */
    @GetMapping("/inner/incrHotScore/{skuId}")
    public Result incrHotScore(@PathVariable Long skuId) {
        searchService.incrHotScore(skuId);
        return Result.ok();
    }


    /**
     * 商品检索
     * /api/list
     * @param searchParam
     * @return
     */
    @PostMapping
    public Result list(@RequestBody SearchParam searchParam) {
        SearchResponseVo searchResponseVo = searchService.search(searchParam);
        return Result.ok(searchResponseVo);
    }
}
